import java.awt.GridLayout;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.data.general.DefaultPieDataset;


public class Download_csv extends Thread
{
	private Database database;
	private String file_in;			// csv
	private String file_out;		// stat.csv
	
	public Download_csv (Database db, String fi, String fo)
	{
		database = db;
		file_in = fi;
		file_out = fo;
	}
	
	public void run() 
	{
		try 
		{
			String name = database.getName();
			
			Interface.textZone.setText(Interface.textZone.getText().concat("\nRetrieving data from "+name+" CSV file ..."));
			GenerateFile.load(database,  file_in);
			Interface.textZone.setText(Interface.textZone.getText().concat("\nData from "+name+" are now stored."));
			
			Interface.textZone.setText(Interface.textZone.getText().concat("\nComputing statistics for "+name+" database.\n")); 	
			
			if(database.getName().equals("Eukarya"))
			{
				Interface.progressBar1.setValue(100);
				Interface.pourcent1.setText("100%");
				
				Interface.stats_eukarya = GenerateFile.stats(database, file_out);
				
				DefaultPieDataset data1 = new DefaultPieDataset();
				Interface.stats_eukarya.graphbest(data1);
		        Interface.chart1 = ChartFactory.createPieChart3D( "Eukarya - Anticodon", data1, true/*legend?*/,false/*tooltips?*/, false/*URLs?*/);
		        Interface.chart1.getPlot().setForegroundAlpha(0.8f);
		        
		        ChartPanel chartPanel1 = new ChartPanel(Interface.chart1);			        
		        Interface.panel_south1.setLayout(new GridLayout());
		        Interface.panel_south1.add(chartPanel1);
			}
			else
			if(database.getName().equals("Archaea"))
			{
				Interface.progressBar2.setValue(100);
				Interface.pourcent2.setText("100%");
				
				Interface.stats_archaea = GenerateFile.stats(database, file_out);
				//System.out.println("END");
				
				DefaultPieDataset data2 = new DefaultPieDataset();
				Interface.stats_archaea.graphbest(data2);
		        Interface.chart2 = ChartFactory.createPieChart3D( "Archaea - Anticodon", data2, true/*legend?*/,false/*tooltips?*/, false/*URLs?*/);
		        Interface.chart2.getPlot().setForegroundAlpha(0.8f);
		        
		        ChartPanel chartPanel2 = new ChartPanel(Interface.chart2);
		        Interface.panel_south2.setLayout(new GridLayout());
		        Interface.panel_south2.add(chartPanel2);
			}	
			else
			if(database.getName().equals("Bacteria"))
			{
				Interface.progressBar3.setValue(100);
				Interface.pourcent3.setText("100%");
				
				Interface.stats_bacteria = GenerateFile.stats(database, file_out);
				
				DefaultPieDataset data3 = new DefaultPieDataset();
				Interface.stats_bacteria.graphbest(data3);
		        Interface.chart3 = ChartFactory.createPieChart3D( "Bacteria - Anticodon", data3, true/*legend?*/,false/*tooltips?*/, false/*URLs?*/);
		        Interface.chart3.getPlot().setForegroundAlpha(0.8f);
		        
		        ChartPanel chartPanel3 = new ChartPanel(Interface.chart3);
		        Interface.panel_south3.setLayout(new GridLayout());
		        Interface.panel_south3.add(chartPanel3);
			}
				
			// Refresh the window
			(Interface.getFrames())[0].setVisible(true);
			Interface.textZone.setText(Interface.textZone.getText().concat("\nStatistics done for "+name+" database.\n"));
		} 
		catch (Exception e) 
		{}
	}
}